package edu.isis.bravefox.ms;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

import com.lucasF.JCommUSB_2_0.USBDevice;
import com.lucasF.JCommUSB_2_0.USBException;

public class MissileServer {
	
	static boolean connected = false;
	static USBDevice myUSB;
	
	static File usb = new File("USB\\\\VID_2123&PID_1010&REV_0001,USB\\\\VID_2123&PID_1010");
	static FileOutputStream usbWriter;
	
	final static byte zero = 0x00;
	final static byte up = 0x01;
	final static byte down = 0x02;
	final static byte left = 0x04;
	final static byte right = 0x08;
	final static byte fire = 0x10;
	
	
	final static int fireCoordX = 125;
	final static int fireCoordY = 150;
	final static int vertDist = 60;
	final static int horizDist = 60;
	
	static Robot clicker;
	
	public static void main(String[] args) throws AWTException {

		
		clicker = new Robot();
		
		clicker.mouseMove(fireCoordX,fireCoordY);
		System.out.println("Ensure the mouse is over the fire button");
		
		DatagramSocket socket = null;
		try {
			socket = new DatagramSocket(27000);
		} catch (SocketException e1) {
			e1.printStackTrace();
		} 
		
		String password = "password";
		
		System.out.println("Enter a password");
		
		InputStreamReader input = new InputStreamReader(System.in);
		BufferedReader reader = new BufferedReader(input);
		
		try {
			password = reader.readLine();
			System.out.println("Waiting for A Shooter...");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		if(socket == null)
			System.exit(-3);
		
		/*try {
			usbWriter = new FileOutputStream(usb);
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}*/
		
		
		/*String szPath;
		try {
			szPath = USBDevice.getAttachedDevicePath(0);
			System.out.println("Device Path of Device at index 0 = "+ szPath);
			myUSB = new USBDevice(szPath);
			System.out.println("connected?"+myUSB.deviceIsConnected());
		} catch (USBException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}*/
		
		
		while(true)
		{
			byte[] receiveBytes = new byte[128];
			DatagramPacket packet = new DatagramPacket(receiveBytes, 128);
			try {
				socket.receive(packet);
				
				InetAddress clientAddress = packet.getAddress();
				int clientPort = packet.getPort();
				
				
				InputStreamReader pRead = new InputStreamReader(new ByteArrayInputStream(receiveBytes));
				BufferedReader packetReader = new BufferedReader(pRead);
				
				String line = packetReader.readLine();
				System.out.println(line);
				
				String[] words = line.split(" ");
				
				if(!connected)
				{
					if(words[1].contains(password))
					{
						System.out.println("Access Granted");
					
						byte[] sendBytes = "control ok".getBytes();
						DatagramPacket sendPacket = new DatagramPacket(sendBytes, sendBytes.length);
						sendPacket.setAddress(clientAddress);
						sendPacket.setPort(clientPort);
						socket.send(sendPacket);	
					
						connected = true;
					}
					else
					{
						System.out.println("Access Denied, Bad password");
						
						byte[] sendBytes = "control no".getBytes();
						DatagramPacket sendPacket = new DatagramPacket(sendBytes, sendBytes.length);
						sendPacket.setAddress(clientAddress);
						sendPacket.setPort(clientPort);
						socket.send(sendPacket);
					}
				}
				else
				{
	
					if(words[0].contains("up"))
					{
						up();
						Thread.sleep(50);
					}
					else if(words[0].contains("down"))
					{
						down();
						Thread.sleep(50);
					}
					else if(words[0].contains("left"))
					{
						left();
						Thread.sleep(50);
					}
					else if(words[0].contains("right"))
					{
						right();
						Thread.sleep(50);
					}
					else if(words[0].contains("fire"))
					{
						fire();
						Thread.sleep(50);
					}
					else if(words[0].contains("disconnect"))
					{
						connected = false;
						Thread.sleep(50);
					}
					else
					{
						//do nothing
						Thread.sleep(50);
					}
				
				}
				
			} catch (IOException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (USBException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}


	private static void up() throws USBException, IOException {

		byte[] bytes = {zero, up, zero};

		//usbWriter.write(bytes);
		
		click(fireCoordX, fireCoordY - vertDist);
		
		//myUSB.writePipeBulkInterrupt(0, 0, bytes, 0, 3);
		
	}

	private static void down() throws USBException, IOException {
		
		byte[] bytes = {zero, down, zero};

		//usbWriter.write(bytes);
		
		click(fireCoordX, fireCoordY + vertDist);
		
//		myUSB.writePipeBulkInterrupt(0, 0, bytes, 0, 3);
	}

	private static void left() throws USBException, IOException {

		byte[] bytes = {zero, left, zero};

		//usbWriter.write(bytes);
		
		click(fireCoordX - horizDist, fireCoordY);
		
	//	myUSB.writePipeBulkInterrupt(0, 0, bytes, 0, 3);
		
	}


	private static void right() throws USBException, IOException {

		byte[] bytes = {zero, right, zero};

		//usbWriter.write(bytes);
		
		click(fireCoordX + horizDist, fireCoordY);
		
//		myUSB.writePipeBulkInterrupt(0, 0, bytes, 0, 3);
		
	}


	private static void fire() throws USBException, IOException {
		
		byte[] bytes = {zero, fire, zero};
		
		//usbWriter.write(bytes);
		
		click(fireCoordX, fireCoordY);
		
	//	myUSB.writePipeBulkInterrupt(0, 0, bytes, 0, 3);
		
	}
	
	private static void click(int x, int y)
	{
		clicker.mouseMove(x, y);
		clicker.mousePress(InputEvent.BUTTON1_MASK);
		clicker.mouseRelease(InputEvent.BUTTON1_MASK);
		
	}

}
